package com.golaberto.client;

import com.golaberto.client.FilteredGrid.ListItemsCallback;

import com.google.gwt.user.client.HTTPRequest;
import com.google.gwt.user.client.ResponseTextHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONException;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Button;

import java.util.ArrayList;

public class Players extends WorkPanel implements ListItemsCallback {
  private FilteredGrid lb_ = new FilteredGrid(20, 5, true);

  public static WorkPanelInfo init() {
    return new WorkPanelInfo("Players", "Players") {
      public WorkPanel createInstance() {
        return new Players();
      }
    };
  }

  public Players() {
    final DockPanel panel = new DockPanel();
    panel.setWidth("100%");

    lb_.setHeader(0, "Name");
    lb_.setHeader(1, "Position");
    lb_.setHeader(2, "Birth");
    lb_.setHeader(3, "Country");
    lb_.setHeader(4, "Full name");
    lb_.setWidth("100%");
    lb_.setListItemsCallback(this);
    lb_.updateGrid();
    panel.add(lb_, DockPanel.CENTER);

    HorizontalPanel bottom_panel = new HorizontalPanel();
    Button load = new Button("Load");
    bottom_panel.add(load);

    Button del = new Button("Delete");
    bottom_panel.add(del);

    Button new_button = new Button("New");
    bottom_panel.add(new_button);

    panel.add(bottom_panel, DockPanel.SOUTH);
    initWidget(panel);
  }

  public void getItems(String text, int page, int count) {
    if (!HTTPRequest.asyncGet(
             "current_players.php?text=" + text + "&page=" + page + "&count=" + count,
             new JSONResponseTextHandler())) {
      Window.alert("Failed");
    }      
  }

  private class JSONResponseTextHandler implements ResponseTextHandler {
    public void onCompletion(String responseText) {
      //lb_.addItem("Completed");
      try {
        JSONValue jsonValue = JSONParser.parse(responseText);
        displayJSONObject(jsonValue);
      } catch (JSONException e) {
        displayError(responseText);
      }
    }
  }

  private void displayError(String responseText) {
    Window.alert(responseText);
  }

  private void displayJSONObject(JSONValue jsonValue) {
    JSONArray jsonArray;
    if ((jsonArray = jsonValue.isArray()) != null) {
      ArrayList matches = new ArrayList();
      for (int i = 0; i < jsonArray.size(); ++i) {
        String[] row = new String[5];
        JSONArray object = jsonArray.get(i).isArray();
        row[0] = object.get(1).isString().stringValue();
        row[1] = object.get(2).isString().stringValue();
        row[2] = object.get(3).isString().stringValue();
        row[3] = object.get(4).isString().stringValue();
        row[4] = object.get(5).isString().stringValue();
        
        matches.add(row);
      }
      String[][] items = new String[matches.size()][5];
      for (int i = 0; i < matches.size(); ++i) {
        items[i] = (String[])matches.get(i);
      }
      lb_.onGetItemsDone(items);
    }
  }

  public void onShow() {
  }
}
